package com.huodongrili.calender.data;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.huodongrili.calender.BuildConfig;
import com.huodongrili.calender.R;
import com.huodongrili.calender.util.ECLog;

import android.content.Context;
import android.content.SharedPreferences;

public class DataPreferences {
	
	private final String CITY_CODE="city_code";
	private final String WIFI_SYNC="wifi_sync";
	private final String FIRST_RUN="first_run";
	private final String LAST_TIMPSTAMP_EVENT="last_tt_event";
	private final String TEMPERATURE="temperature";
	private final String TEMP_ICON="temperature_ic";
	private final String LOAD_LUNAR_TIMPSTAMP="llt";
	private final String LAST_UPDATE_WEATHER_TIME="luwt";
	private final String SYNC_TIME="sync_time";
	
	
	private SharedPreferences.Editor mEditor;
	private SharedPreferences mPref;
	private static DataPreferences dataPre;

	private DataPreferences(Context context) {
		SharedPreferences localSharedPreferences = context.getSharedPreferences("ec_conf", Context.MODE_PRIVATE);
		mPref = localSharedPreferences;
		mEditor = mPref.edit();
	}

	public static DataPreferences getInstance(Context context) {
		if (dataPre == null) {
			dataPre = new DataPreferences(context);
		}
		return dataPre;
	}
	
	
	/**
	 * 
	 * @param time
	 */
	public void setSyncTime(long time){
		mEditor.putLong(SYNC_TIME, time);
		mEditor.commit();
	}
	
	public long getSyncTime(){
		return mPref.getLong(SYNC_TIME, System.currentTimeMillis());
	}
	
	
	/**
	 * set the last time which get weather
	 * @param time
	 */
	public void setLoadWeatherTime(){
		
		Calendar c = Calendar.getInstance();
		int month = c.get(Calendar.MONTH);
		int year = c.get(Calendar.YEAR);
		int day = c.get(Calendar.DAY_OF_MONTH);

		SimpleDateFormat dateFormat = new SimpleDateFormat(Constant.DATE_FORMAT);
		Date todayDate= new Date(year-1900, month, day);
		String saveDate = dateFormat.format(todayDate);
		
		mEditor.putString(LAST_UPDATE_WEATHER_TIME, saveDate);
		mEditor.commit();
	}
	
	public boolean getLoadWeatherTime(){
		
		Calendar c = Calendar.getInstance();
		int month = c.get(Calendar.MONTH);
		int year = c.get(Calendar.YEAR);
		int day = c.get(Calendar.DAY_OF_MONTH);

		SimpleDateFormat dateFormat = new SimpleDateFormat(Constant.DATE_FORMAT);
		Date todayDate= new Date(year-1900, month, day);
		String TodayDateStr = dateFormat.format(todayDate);
		
		String reStr = mPref.getString(LAST_UPDATE_WEATHER_TIME, TodayDateStr);
		
		Date lastDate;
		try {
			lastDate = dateFormat.parse(reStr);
			
			if(BuildConfig.DEBUG){
				ECLog.debug("TodayDateStr:"+TodayDateStr+",lastDateStr:"+reStr);
			}
			
			
			if(todayDate.equals(lastDate)){
				
				if(BuildConfig.DEBUG){
					ECLog.debug("TodayDateStr====lastDateStr");
				}
				
				return true;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return false;
	}
	
	
	
	/**
	 * set the last time which get evetns
	 * @param time
	 */
	public void setLoadLunarTime(long time){
		mEditor.putLong(LOAD_LUNAR_TIMPSTAMP, time);
		mEditor.commit();
	}
	
	public long getLoadLunarTime(){
		return mPref.getLong(LOAD_LUNAR_TIMPSTAMP, System.currentTimeMillis()/1000);
	}
	
	
	/**
	 * 
	 * @param time
	 */
	public void setWeatherIcon(int resId){
		mEditor.putInt(TEMP_ICON, resId);
		mEditor.commit();
	}
	
	public int getWeatherIcon(){
		return mPref.getInt(TEMP_ICON,-1);
	}
	
	
	/**
	 * 
	 * @param time
	 */
	public void setTemperature(String time){
		mEditor.putString(TEMPERATURE, time);
		mEditor.commit();
	}
	
	public String getTemperature(){
		return mPref.getString(TEMPERATURE, "");
	}
	
	
	/**
	 * set the last time which get evetns
	 * @param time
	 */
	public void setGetEventTime(long time){
		mEditor.putLong(LAST_TIMPSTAMP_EVENT, time);
		mEditor.commit();
	}
	
	public long getEventTime(){
		return mPref.getLong(LAST_TIMPSTAMP_EVENT,  System.currentTimeMillis()/1000);
	}
	
	
	/**
	 * set is first run
	 * @param isSync
	 */
	public void setFirstRun(boolean is){
		mEditor.putBoolean(FIRST_RUN, is);
		mEditor.commit();
	}
	
	public boolean isFirstRun(){
		return mPref.getBoolean(FIRST_RUN, true);
	}
	
	/**
	 * set auto sync under wifi staus
	 * @param isSync
	 */
	public void setWifiSync(boolean isSync){
		mEditor.putBoolean(WIFI_SYNC, isSync);
		mEditor.commit();
	}
	
	public boolean isWifiSync(){
		return mPref.getBoolean(WIFI_SYNC, true);
	}
	
	
	/**
	 * set city 
	 * @param code
	 */
	public void setCityCode(String code){
		mEditor.putString(CITY_CODE, code);
		mEditor.commit();
	}
	
	public String getCityCode(){
		return mPref.getString(CITY_CODE, Constant.CODE_BJ);
	}

}
